/*
 *  Document   : base_forms_validation.js
 *  Author     : pixelcave
 *  Description: Custom JS code used in Form Validation Page
 */

var BaseFormValidation = function () {
    // Init Bootstrap Forms Validation, for more examples you can check out https://github.com/jzaefferer/jquery-validation
    var initValidationBootstrap = function () {
        jQuery('#postFrom').validate({
            ignore: [],
            errorClass: 'help-block text-left animated fadeInDown',
            errorElement: 'div',
            errorPlacement: function (error, e) {
                jQuery(e).parent().append(error);
            },
            highlight: function (e) {
                var elem = jQuery(e);

                elem.closest('.form-group').removeClass('has-error').addClass('has-error');
                elem.closest('.help-block').remove();
            },
            success: function (e) {
                var elem = jQuery(e);

                elem.closest('.form-group').removeClass('has-error');
                elem.closest('.help-block').remove();
            },
            rules: {
                'station-name': {
                    required: true,
                    minlength: 2,
                    maxlength: 20,
                },
                'page-url': {
                    required: true,
                    isUrl: true,
                },
                'jump-url': {
                    required: true,
                    isHttpUrl: true,
                },
                'station-type': {
                    required: true,
                    min: 1,
                },

            },
            messages: {
                'station-name': {
                    required: '请输入站点名称',
                    minlength: '站点名称最少2个字符',
                    maxlength: '站点名称最多20个字符'
                },
                'page-url': {
                    required: '请输入域名',
                    isHttpUrl: '请输入正确的域名',
                },
                'jump-url': {
                    required: '请输入跳转地址',
                    isHttpUrl: '请输入带http或https的跳转地址',
                },
                'station-type': {
                    required: '请选择站点类型',
                },

            }
        });
    };

    var compareForm=function(f1,f2){
        console.log(f1);
        console.log(f2);
        if(f1.name!=f2.name) return false;
        if(f1.domain!=f2.domain) return false;
        if(f1.url!=f2.url) return false;
        if(f1.device!=f2.device) return false;
        if(f1.arearfuse!=f2.arearfuse) return false;
        if(f1.type!=f2.type) return false;
        if(f1.allowqq!=f2.allowqq) return false;
        if(f1.allowweixin!=f2.allowweixin) return false;
        if(f1.areas.length!=f2.areas.length) return false;
        for(var i=0;i<f1.areas.length;i++){
            if(f1.areas[i].code!=f2.areas[i].code)
                return false;
        }
        return true;
    };
    var getForm=function () {
        var form=new Object();

        form.name=$('input[name="station-name"]').val();
        form.domain=$('input[name="page-url"]').val();
        form.url=$('input[name="jump-url"]').val();
        form.allowqq=$('input[name="allowqq"]').prop('checked')?1:0;
        form.allowweixin=$('input[name="allowweixin"]').prop('checked')?1:0;
        var $arearfuse=$('input[name="area-config"]');
        $.each($arearfuse,function (key,index) {
            if($(index).prop('checked'))
                form.arearfuse=parseInt($(index).val());
        })
        var $device=$('input[name="device-type"]');
        $.each($device,function (key,index) {
            if($(index).prop('checked'))
                form.device=parseInt($(index).val());
        })
        var areas=[];
        $.each($('#area-display .label'),function(key,index){
            var data=$(index).attr('data-for');
            var arr=data.split('-');
            areas[key]={
                'code':parseInt(arr[0]),
                'name':arr[1]
            };

        });
        form.areas=areas;
        form.type=parseInt($('select[name="station-type"]').val());
        return form;
    }
    return {
        init: function () {
            // Init Bootstrap Forms Validation
            initValidationBootstrap();
            jQuery.areaSelect('#select-area', '#selected-area', areas);
            jQuery('#submitForm').on('click', function () {
                var vail = jQuery('#postFrom').valid();
                if (!vail) return false;
                jQuery(this).attr('disabled', 'true');
                var form=getForm();
                if(compareForm(form,formInit)){
                    layer.msg('站点信息未改变!');
                    jQuery(this).removeAttr('disabled', 'true');
                    return;
                }
                var index = layer.load(0, {shade: false});
                jQuery.ajax({
                    type:'post',
                    url:pages.post_url,
                    data:jQuery('#postFrom').serialize(),
                    dataType:'json',
                    async: false,
                    success:function(data){
                        layer.close(index);
                        if(data.status!=1){
                            layer.close(index);
                            jQuery('#submitForm').removeAttr('disabled');
                            layer.msg(data.msg, function(){});
                        }else{
                            layer.msg('修改成功', {icon: 1});
                            jQuery('#submitForm').removeAttr('disabled', 'true');
                            setTimeout(function(){
                                location.reload();
                            },500);
                        }
                    },
                    error:function(){
                        layer.close(index);
                        jQuery('#submitForm').removeAttr('disabled');
                        layer.msg('网络错误', function(){});
                    }

                })
            });
            jQuery('#page-url').on('blur',function(){
                var url=$(this).val();
                var regx=/(([A-Za-z0-9-~]+)\.)+(com|net|cn|com|\.cn|cc|top|wang|vip|tv|xyz|xin|club|info)/g;
                var arr=url.match(regx);
                if(arr&&arr[0]){
                    $(this).val(arr[0]);
                    jQuery('#postFrom').validate().element('#page-url');
                }

            });
        }
    };
}();

// Initialize when page loads
jQuery(function () {
    BaseFormValidation.init();
});




